/**
 * 
 */
package org.baixin.main;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;

import org.baixin.action.BaseActionSupport;
import org.baixin.utils.CodeUtils;
import org.springframework.stereotype.Service;

/**
 * @author slaton
 *
 * 下午03:12:06
 */
@Service("mainAction")
public class MainAction extends BaseActionSupport {

	private ByteArrayInputStream inputStream;
	
	public String main(){
		
		return SUCCESS;
	}
	
	@Override
	public String delete() throws Exception {
		
		return null;
	}

	
	@Override
	public String input() throws Exception {
		
		return null;
	}


	@Override
	public String list() throws Exception {
		
		return null;
	}

	
	@Override
	protected void prepareModel() throws Exception {
		
		
	}


	@Override
	public String save() throws Exception {
		
		return null;
	}


	@Override
	public Object getModel() {
		
		return null;
	}
	
	public String validateCode() throws IOException{
		int width = 85; 
		int height = 19;
	
		BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB); 
		Graphics g = image.getGraphics();
		g.setColor(Color.WHITE);
		g.fillRect(0, 0, width, height);
		g.setFont(new Font("Lucida Console", Font.PLAIN, 18));
		String[] code = CodeUtils.createCode(5, true, true);
		StringBuffer sb = new StringBuffer();
		for (int i=0; i<code.length;i++) {
			g.setColor(CodeUtils.getRandomColor());
			sb.append(code[i]);
			g.drawString(code[i], (int)(width*0.17)*(i+1), (int)(height*0.8));
		}
		request.getSession().setAttribute("validateCode", sb.toString());
		logger.debug("validateCode is "+sb.toString());
		g.dispose();
		ByteArrayOutputStream output = new ByteArrayOutputStream();  
        ImageOutputStream imageOut = ImageIO.createImageOutputStream(output);  
        ImageIO.write(image, "JPEG", imageOut);  
        imageOut.close();  
        ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray());  
        this.inputStream = input; 
        return SUCCESS;
	}
	
	public ByteArrayInputStream getInputStream() {  
        return inputStream;  
    }
}
